package com.example.administrator.zhongyichou.activity;

import android.view.View;
import android.widget.TextView;

import com.example.administrator.zhongyichou.adapter.WaitPayListAdapter;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.listener.ResultListener;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.net.protocol.WaitPayProtocol;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.example.administrator.zhongyichou.view.MyListView;
import com.example.administrator.zhongyichou.R;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lichenyu on 2017/11/23.
 * 描述：加入行动第二个页面
 */

public class WaitPayActivity extends BaseActivity<WaitPayProtocol> {

    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.listview)
    MyListView listview;
    @BindView(R.id.txt_pay)
    TextView txtPay;
    @BindView(R.id.txt_price)
    TextView txtPrice;
    private String member_id;
    private WaitPayListAdapter adapter;
    private double price = 0.01;
    private IWXAPI api;
    private int mutualType = 0;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
        initWechat();
    }

    @Override
    protected void responseData(WaitPayProtocol mGsonBaseProtocal) {
        adapter = new WaitPayListAdapter(this, mGsonBaseProtocal.getData().getUser_list());
        listview.setAdapter(adapter);
        price = mGsonBaseProtocal.getData().getTotal_fee();
        txtPrice.setText(price + "");
    }

    @Override
    protected HttpLoader initHttpLoader() {
        mutualType = getIntent().getIntExtra("mutual_type", 0);
        member_id = getIntent().getStringExtra("member_id");
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(WaitPayProtocol.class);
        Map map = new HashMap();
        map.put("member_id", member_id);
        map.put("mutual_type", mutualType + "");
        map.put("user_id", MyApplication.getUserID());
        requestHttpLoader.setParams(NetConfig.WAIT_PAY, map);
        return requestHttpLoader;
    }

    @Override
    protected boolean isRequestData() {
        return true;
    }

    @Override
    protected int initContentView() {
        return R.layout.join_action_next_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {

        return false;
    }

    /**
     * 初始化微信支付api
     */
    private void initWechat() {
        api = WXAPIFactory.createWXAPI(this, "wxb4ba3c02aa476ea1");
    }

    private void initHead() {
        headerview.setTitle("加入行动");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                }
            }
        });
    }

    @OnClick({R.id.txt_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_pay:
//                pay();
                break;
        }
    }

    /**
     * 支付
     */
    private void pay() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(GsonBaseProtocol.class);
        Map map = new HashMap();
        map.put("user_id", MyApplication.getUserID());
        map.put("member_id", member_id);
        map.put("recharge_money", price + "");
        map.put("trade_type", "APP");
        requestHttpLoader.setParams(NetConfig.PAY_MUTUAL, map);
        requestHttpLoader.executor(new ResultListener() {
            @Override
            public void success(GsonBaseProtocol mGsonBaseProtocol) {

            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }


//    /**
//     * 调用微信支付
//     */
//    public void sendPayRequest() {
//        PayReq req = new PayReq();
//        req.appId = weiXinBean.getAppid();
//        req.partnerId = weiXinBean.getPartnerid();
//        //预支付订单
//        req.prepayId = weiXinBean.getPrepayid();
//        req.nonceStr = weiXinBean.getNoncestr();
//        req.timeStamp = weiXinBean.getTimestamp() + "";
//        req.packageValue = weiXinBean.getPackageValue();
//        req.sign = weiXinBean.getSign();
//        // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
//        //3.调用微信支付sdk支付方法
//        api.sendReq(req);
//    }
}